# test/CMakeLists.txt
# Alan Heirich 1 July 2010
#

cmake_minimum_required( VERSION 2.6 )

add_custom_target( test )

set( CMAKE_VERBOSE_MAKEFILE 1 )

file( GLOB pdl_files *.pdl ../examples/*.pdl )

foreach( f ${pdl_files} )
  get_filename_component( f1 ${f} PATH )
  get_filename_component( f2 ${f} NAME )
  set( filenamepath ${f1}/${f2} )
  get_filename_component( filenameext ${f} NAME )
  get_filename_component( filename ${filenameext} NAME_WE )
  set( mainFile ${filenameext}.main.cpp )
  set( hppFile ${filename}.hpp )
  set( clFile ${filename}.cl )
  set( testFile ${filename}.test.cpp )
  set( testlogFile ${filenameext}.testlog )
  set( extFile ${filename}.ext.cpp )
  set( executable ${filename} )
  set( verifier ${filename}.verify.py )
  set( verifyFile ${filenameext}.verify )
  MESSAGE( "Found one unit test " ${filenameext} )

  # main
  if( EXISTS test/${mainFile} )
	MESSAGE( "  unit test " ${filenameexit} " has main program " ${mainFile} )
  else()
	add_custom_command( 
		OUTPUT ${mainFile}
		COMMAND echo generating boilerplate main program ${mainFile}
		COMMAND cat UnitTestMain.cpp|sed -e 's:__P1:\"${hppFile}\":g'|sed -e 's:__P2:${filename}:g'|sed -e 's:__P3:\"${filename}\":g' > ${mainFile}
		DEPENDS UnitTestMain.cpp
		)
  endif()

  # run pipelinekit
  MESSAGE( "  pdl source " ${filenamepath} )
  add_custom_command( 
    OUTPUT ${hppFile} ${testFile} ${clFile}
	COMMAND ${EXECUTABLE} ${filenamepath} > ${filenameext}.log 
	DEPENDS ${EXECUTABLE} ${filenamepath} stringify.exe
	)

  # test-specific stages
  if( EXISTS test/${extFile}.save )
    MESSAGE( "  unit test " ${filenameext} " has custom externals " ${extFile} )
    add_custom_command( 
	  OUTPUT ${extFile}
	  COMMAND rm -f ${extFile}
	  COMMAND cp ${extFile}.save ${extFile}
	  COMMAND echo using stored ${extFile}
	  DEPENDS ${extFile}.save ${hppFile}
	  )
  else()
    add_custom_command( 
      OUTPUT ${extFile} 
      COMMAND echo using generated ${extFile} 
      DEPENDS ${hppFile}
      )
  endif()

  MESSAGE( "  adding executable " ${executable} )
  add_executable( ${executable} ${mainFile} ${hppFile} ${extFile} ${testFile} )
  add_executable( ${executable}.dbg ${mainFile} ${hppFile} ${extFile} ${testFile} )

  set_target_properties( ${executable}.dbg PROPERTIES COMPILE_FLAGS -g )
  
  if( ${WIN32} )
    target_link_libraries( ${executable} OpenCL )
    target_link_libraries( ${executable}.dbg OpenCL )
  elseif( ${APPLE} )
    target_link_libraries( ${executable} ${OPENCL_ROOT}/OpenCL )
    target_link_libraries( ${executable}.dbg ${OPENCL_ROOT}/OpenCL )
  elseif( ${UNIX} )
    target_link_libraries( ${executable} OpenCL )
    target_link_libraries( ${executable}.dbg OpenCL )
  endif()

  # test log
  add_custom_target( ${testlogFile}
    COMMAND ./${executable}  > ${testlogFile}
    COMMAND ls -l *.bin *.txt | tee >> ${testlogFile}
    COMMAND ls -l ${testlogFile}
    DEPENDS ${executable}
    )

  # verify the test result
  add_custom_target( ${verifyFile}
    COMMAND echo running python ${verifier}
	COMMAND cat ${testlogFile} | python ${verifier} &> ${verifyFile}
	DEPENDS test/${testlogFile} ${verifier}
	)
	add_dependencies( test ${verifyFile} )
endforeach( f )

